﻿using System;
using System.Collections.Generic;
using System.Text;
using Tracy.CoreLib.Data;
using Tracy.CoreLib;
using Tracy.Agent.DownloaderAdapter;
using CoreLib.Message;

namespace Tracy.Engine
{
    public class DownloadDispatcher : MessageProcessor
    {
        public event EventHandler<EventArgsWithData> OnException;
        public event EventHandler<TracyMessageEventArgs> OnMessageProcessed;
        public event EventHandler<TracyGeneralEventArgs> OnQueueModified;
        protected override void CallOnMessageProcessed(object sender, TracyMessageEventArgs e)
        {
            OnMessageProcessed(sender, e);
        }
        protected override void CallOnQueueModified(object sender, TracyGeneralEventArgs e)
        {
            OnQueueModified(sender, e);
        }

        public override bool ExitUntilQueueCleared
        {
            get
            {
                return false;//Force Exit
            }
        }
        private TracyBus bus = null;
        /// <summary>
        /// 总线
        /// </summary>
        private TracyBus Bus
        {
            get { return bus; }
        }

        Dictionary<string, string> downloaderMapping = new Dictionary<string,string> ();
        Dictionary<string, IDownloaderAdapter> downloaders;

        private Dictionary<string, IDownloaderAdapter> Downloaders
        {
            get { if (downloaders == null)downloaders = new Dictionary<string, IDownloaderAdapter>(); return downloaders; }
        }

        public void ShutDown()
        {
            foreach (IDownloaderAdapter adapter in Downloaders.Values)
            {
                adapter.ShutDown();
            }
            killThreadFlag = true;
        }

        public DownloadDispatcher(TracyBus bus)
        {
            this.bus = bus;
            downloaderMapping.Add(Constants.CONST_VALUE_TYPE_EMULE, Bus.TracySettings[Constants.CONST_SETTING_DOWNLOADER_EMULE]);
            downloaderMapping.Add(Constants.CONST_VALUE_TYPE_BT, Bus.TracySettings[Constants.CONST_SETTING_DOWNLOADER_BT]);
            //TODO:增加其他协议
        }

        /// <summary>
        /// 按类型分发消息
        /// </summary>
        protected override void ProcessMessage(ITracyMessage msg)
        {
            try
            {
                if (msg is DownloadMessage) ProcessMessage((DownloadMessage)msg);
                //TODO:增加更多的消息类型
            }
            catch (Exception e)
            {
                msg.MessageException = e;
            }
        }

        public void ProcessMessage(DownloadMessage msg)
        {
            IDownloaderAdapter adapter = GetDownloaderAdapter(msg.Item);
            adapter.KeepAlive();
            adapter.AddToDownload(msg.Item);
        }

        //public bool AddToDownload(ResourceItem item)
        //{
        //    EnqueueMessage(
        //    return true;
        //}

        private IDownloaderAdapter GetDownloaderAdapter(ResourceItem item)
        {
            if (!Downloaders.ContainsKey(item.Type))
            {
                Downloaders[item.Type] = Bus.TracyPlugins.CreateDownloaderAdapter(downloaderMapping[item.Type]);
                Downloaders[item.Type].OnException += new EventHandler<EventArgsWithData>(DownloadDispatcher_OnException);
            }
            return Downloaders[item.Type];
        }

        void DownloadDispatcher_OnException(object sender, EventArgsWithData e)
        {
            OnException(sender, e);
        }
    }
}
